<?php
declare (strict_types=1);

namespace addons\Pay\driver;

use addons\Pay\exception\PayException;
use We;

class AliPay
{

    public static function create($config, $trade_type)
    {
        try {
            $method = 'AliPay' . $trade_type;
            $config->config['alipay']['notify_url'] = $config->notify_url;
            $config->config['alipay']['return_url'] = $config->redirect_url;
            switch ($trade_type) {
                case "Web":
                case "Wap":
                case "Scan":
                case "App":
                    $pay = We::$method($config->config['alipay']);
                    $options = $pay->apply([
                        'out_trade_no' => $config->out_trade_no,
                        'total_amount' => $config->total,
                        'subject'      => $config->subject,
                    ]);
                    break;
                default:
                    throw new PayException('trade_type为空或不支持其支付方式' - 1);
            }
            return $options;
        } catch (\Exception $e) {
            throw new PayException('创建订单失败：' . $e->getMessage(), -1);
        }
    }

}